package com.demo.springcloud.run;


import com.demo.springcloud.common.Request;
import com.demo.springcloud.common.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

@Component
public class SayHi {

    Random random = new Random(System.currentTimeMillis());
    @Autowired
    RestTemplate restTemplate;

//    @Scheduled(cron = "0/1 * * * * ?")
    public void call(){
        Map<String, Object> param = new HashMap<>();
        int count = random.nextInt(16);
        param.put("count",count);
        ResponseEntity<Integer> result = restTemplate.getForEntity("http://MODULE-01/double/2?count={count}",Integer.class,param);
        System.out.println(String.format("%d * 2 = %d",count,result.getBody()));

    }

//    @Scheduled(cron = "0/1 * * * * ?")
    public void call2(){
        Map<String, Object> param = new HashMap<>();
        int count = random.nextInt(16);
        param.put("count",count);
        ResponseEntity<Integer> result = restTemplate.getForEntity("http://MODULE-01/double/3?count={count}",Integer.class,param);
        System.out.println(String.format("%d * 3 = %d",count,result.getBody()));

    }

//    @Scheduled(cron = "0/1 * * * * ?")
    public void call3(){

        Request request = new Request();
        request.setId(random.nextInt(16));
        request.setMessage("这个是请求信息");
        Response result = restTemplate.postForObject("http://MODULE-01/common", request, Response.class);
        System.out.println(result);
    }



}
